package org.example.rtc.controller;

import cn.dev33.satoken.stp.StpUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

@RestController
public class TokenSourceController {

    @GetMapping("/checkTokenSource")
    public String checkTokenSource(HttpServletRequest request) {
        String tokenFromHeader = request.getHeader("satoken");
        String tokenFromCookie = request.getCookies() == null ? null :
                Arrays.stream(request.getCookies())
                        .filter(cookie -> "satoken".equals(cookie.getName()))
                        .findFirst()
                        .map(cookie -> cookie.getValue())
                        .orElse(null);
        String tokenFromParam = request.getParameter("satoken");

        return "Token from Header: " + tokenFromHeader + "\n" +
                "Token from Cookie: " + tokenFromCookie + "\n" +
                "Token from Param: " + tokenFromParam + "\n" +
                "isLogin: " + StpUtil.isLogin();
    }
}
